package ar.uba.dc.cmovil.tripplanner.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

/**
 * Created by anton on 10/12/13.
 */
public class TwitterService {
    private static String TWITTER_APP = "TripPlannerHola";
    private static TwitterService instance;
    private Twitter twitter;


    private TwitterService() {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey("4pldw1wMNO82Yh3y8HPaA")
                .setOAuthConsumerSecret("621JBFxIW5iEbvDwPLru4jeJ1efM2ciWWCaKAjm4")
                .setOAuthAccessToken("1957731240-VRTA6cQBKJkoZEqBbw7mYjfBTpuNaGut33ZA2C2")
                .setOAuthAccessTokenSecret("Tg0egWZZkM7bzNVY3yEEz78mcWIFni3x5oiM5YGqhlM");
        TwitterFactory tf = new TwitterFactory(cb.build());
        twitter = tf.getInstance();
    }

    public static TwitterService getInstance() {
        if (instance == null) {
            instance = new TwitterService();
        }
        return instance;
    }

    public void postPlace(String place) throws TwitterException {
        place = normalizePlace(place);
        twitter.updateStatus("#" + place + " " + new Date());
    }

    public int countTweetsByPlace(String place) {
        place = normalizePlace(place);
        return findTweetsByPlace(place).size();
    }

    private String normalizePlace(String place) {
        place = place.replace(' ', '_');
        return place;
    }

    public Map<String, Integer> countTweetsByPlaces(List<String> places) {
        Map<String, Integer> count = new HashMap<String, Integer>();
        for (String place : places) {
            place = normalizePlace(place);
            count.put(place, 0);
        }
        try {
            List<Status> tweets = twitter.getUserTimeline(TWITTER_APP);
            for (Status status : tweets) {
                for (String place : places) {
                    place = normalizePlace(place);
                    if (status.getText().contains(place)) {
                        count.put(place, count.get(place) + 1);
                    }
                }
            }
        } catch (TwitterException e) {
        }
        return count;
    }

    public List<Status> findTweetsByPlace(String place) {
        place = normalizePlace(place);
        List<Status> result = new ArrayList<Status>();
        try {
            result = twitter.getUserTimeline(TWITTER_APP);
            filterByHashTag(result, place);
        } catch (TwitterException e) {
        }
        return result;
    }

    private void filterByHashTag(List<Status> result, String hashTag) {
        ListIterator<Status> it = result.listIterator();
        hashTag = normalizePlace(hashTag);
        while (it.hasNext()) {
            Status status = it.next();
            if (!status.getText().contains("#" + hashTag)) {
                it.remove();
            }
        }
    }

    public void removeTweetByPlace(String place) {
        place = normalizePlace(place);
        List<Status> result = findTweetsByPlace(place);
        if (result.isEmpty()) {
            return;
        }
        try {
            twitter.tweets().destroyStatus(result.get(0).getId());
        } catch (TwitterException e) {
            e.printStackTrace();
        }
    }
}
